package ch.janotta.slawo.ultraquest.gui.charactergeneration;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ch.janotta.slawo.ultraquest.types.Profession;
import ch.janotta.slawo.ultraquest.types.Species;

public class CharacterGenerationView extends JFrame {
  private CharacterGenerationController controller;

  private JButton button;
  private JPanel panel;
  private JMenuBar speciesMenu;
  private JLabel speciesLabel;
  private JMenuBar professionsMenu;
  private JLabel professionLabel;

  private AttributePanel movementPanel;
  private AttributePanel strengthPanel;
  private AttributePanel dexterityPanel;
  private AttributePanel intelligencePanel;
  
  public CharacterGenerationView(CharacterGenerationController controller) {
    super("Character generation");
    setSize(300, 400);
    setLocation(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(new GridLayout(10,2));

    speciesLabel = new JLabel("-select on left-");
    speciesMenu = getSpeciesSelectionMenu();
    panel.add(speciesMenu);
    panel.add(speciesLabel);

    professionLabel = new JLabel("-select on left-");
    professionsMenu = getProfessionSelectionMenu();
    panel.add(professionsMenu);
    panel.add(professionLabel);

    movementPanel = new AttributePanel("Movement");
    panel.add(movementPanel);
    strengthPanel = new AttributePanel("Strength");
    panel.add(strengthPanel);
    dexterityPanel = new AttributePanel("Dexterity");
    panel.add(dexterityPanel);
    intelligencePanel = new AttributePanel("Intelligence");
    panel.add(intelligencePanel);

    getContentPane().add(panel);
    pack();

    this.controller = controller;
  }

  public void update() {
    
  }

  public void showWindow() {
    setVisible(true);
  }

  public void hideWindow() {
    setVisible(false);
  }


  private JMenuBar getSpeciesSelectionMenu() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Species");
    menuBar.add(menu);
    Species[] species = Species.values();
    for (Species oneSpecies : species) {
      String menuItemLabel = oneSpecies.name().substring(0, 1).toUpperCase() +
          oneSpecies.name().substring(1).toLowerCase();
      JMenuItem menuItem = new JMenuItem(menuItemLabel);
      menuItem.addActionListener(new SpeciesSelectionListener());
      menu.add(menuItem);
    }
    return menuBar;
  }

  private JMenuBar getProfessionSelectionMenu() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Professions");
    menuBar.add(menu);
    Profession[] items = Profession.values();
    for (Profession item : items) {
      String menuItemLabel = item.name().substring(0, 1).toUpperCase() +
          item.name().substring(1).toLowerCase();
      JMenuItem menuItem = new JMenuItem(menuItemLabel);
      menuItem.addActionListener(new MenuSelectionListener(professionLabel));
      menu.add(menuItem);
    }
    return menuBar;
  }


  private class MenuSelectionListener implements ActionListener {
    private JLabel label;

    public MenuSelectionListener(JLabel label) {
      this.label = label;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      if (o instanceof JMenuItem) {
        JMenuItem item = (JMenuItem) o;
        label.setText(item.getText());
      }
    }
  }

  private class SpeciesSelectionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      if (o instanceof JMenuItem) {
        JMenuItem item = (JMenuItem) o;
        speciesLabel.setText(item.getText());
      }
    }
  }
}
